myApp.controller('expressListController',function($scope,toaster,$uibModal,$filter,$state,$log,$serExpress){
    var ctrl = this;
    ctrl.vm = {
        data:{},
        search:{
            id:'',
            startTime:'',
            endTime:'',
            state:'',
            startStamp:'',
            endStamp:''
        },
        searchFn:function(){
            vm.init();
        },
        disable:function(id){
            $serExpress.disable({
                id:id
            }).then(function(res){
                if(res.code==1){
                    vm.init();
                    toaster.info('提示',res.message);
                }else{
                    toaster.info('提示',res.message);
                }
            })
        },
        enable:function(id){
            $serExpress.enable({
                id:id
            }).then(function(res){
                if(res.code==1){
                    vm.init();
                    toaster.info('提示',res.message);
                }else{
                    toaster.info('提示',res.message);
                }
            })
        }
    };
    $scope.vm={
        page:{}
    };
    /*跳转页面*/
    $scope.pageChanged = function() {
        vm.init($scope.vm.page.current_page)
    };
    /*每页显示数量显示*/
    $scope.placement = {
        options: [5,10,20,50],
        pageSize:5,
        selected: 20,
        changeFn:function(){
            vm.init(0)
        }
    };
    $scope.model = {
        open: function (flag) {
            $scope.model.flag = flag;
            var modalInstance = $uibModal.open({
                templateUrl: 'addExpress.html',
                controller: 'addExpressCtrl',
                windowClass: 'expressOpen',
                size:'sm',
                resolve: {
                    info: function () {
                        return $scope.model.flag;
                    }
                }
            });
            modalInstance.result.then(function (res) {
                if (res) {
                   vm.init();
                }
            }, function () {
                $log.info('Modal dismissed at: ' + new Date());
            });
        },
        open1: function (flag) {
            $scope.model.flag = flag;
            var modalInstance = $uibModal.open({
                templateUrl: 'upExpress.html',
                controller: 'upExpressCtrl',
                windowClass: 'expressOpen',
                size:'sm',
                resolve: {
                    info: function () {
                        return $scope.model.flag;
                    }
                }
            });
            modalInstance.result.then(function (res) {
                if (res) {
                    vm.init();
                }
            }, function () {
                $log.info('Modal dismissed at: ' + new Date());
            });
        }
    };
    var vm ={
        init:function(page){
            $serExpress.getList({
                page:page||1,
                size:$scope.placement.selected,
            }).then(function(res){
                if(res.code==1){
                    ctrl.vm.data = res.data;
                   $scope.vm.page = res.addtion_data;
                }else{
                    ctrl.vm.data ="";
                    toaster.info('提示',res.message);
                }
            })
        }
    };
    vm.init();
    myApp.controller('addExpressCtrl', function($scope, $uibModalInstance, info,$serExpress, toaster) {
        $scope.vm = {
            name:'',
            alias:'',
            status:'',
            sort:'',
        }
        $scope.ok = function () {
            $serExpress.addIt({
                name:$scope.vm.name,
                alias:$scope.vm.alias,
                status:$scope.vm.status,
                code:$scope.vm.code,
                sort:$scope.vm.sort
            }).then(function(res){
                if(res.code==1){
                    toaster.info('提示',res.message);
                    $uibModalInstance.dismiss(0);
                    vm.init();
                }else{
                    toaster.info('提示',res.message);
                }
            })
        };

        $scope.cancel = function () {
            $uibModalInstance.dismiss(0);
        };
    });
    myApp.controller('upExpressCtrl', function($scope, $uibModalInstance, info,$serExpress, toaster) {
        $scope.vm = {
            name:'',
            alias:'',
            status:'',
            sort:'',
            code:''
        }
        console.log(info,'编辑');


        function init(){
            $scope.vm.name = info.name
            $scope.vm.alias = info.alias
            $scope.vm.status = info.status
            $scope.vm.sort = info.sort
            $scope.vm.code = info.code
        };
        init();
        $scope.ok = function () {
            $serExpress.updateIt({
                id:info.id,
                name:$scope.vm.name,
                alias:$scope.vm.alias,
                status:$scope.vm.status,
                code:$scope.vm.code,
                sort:$scope.vm.sort
            }).then(function(res){
                if(res.code==1){
                    toaster.info('提示',res.message);
                    $uibModalInstance.dismiss(0);
                    vm.init();
                }else{
                    toaster.info('提示',res.message);
                }
            })
        };

        $scope.cancel = function () {
            $uibModalInstance.dismiss(0);
        };
    });
});

